﻿using System;
using System.Web.Mvc;
using DTcms.Common;

namespace DTcms.Web.Mvc.UI.Page
{
    public partial class payment : BaseController
    {
        protected string action = string.Empty;
        protected string order_no = string.Empty;
        protected string order_type = string.Empty;
        protected decimal order_amount = 0;
        protected string redirect = string.Empty;//回传地址

        protected Model.orderconfig orderConfig = new BLL.orderconfig().loadConfig(); //订单配置
        protected Model.payment payModel; //支付
        
        /// <summary>
        /// 将在Init事件执行
        /// </summary>
        public payment()
        {
            Model.users userModel; //用户
            Model.user_recharge rechargeModel; //充值
            //取得处事类型
            action = DTRequest.GetString("action");
            order_no = DTRequest.GetString("order_no");

            if (order_no.ToUpper().StartsWith("R")) //充值订单
            {
                order_type = DTEnums.AmountTypeEnum.Recharge.ToString().ToLower();
            }
            else if (order_no.ToUpper().StartsWith("B")) //商品订单
            {
                order_type = DTEnums.AmountTypeEnum.BuyGoods.ToString().ToLower();
            }

            switch (action)
            {
                case "confirm":
                    if (string.IsNullOrEmpty(action) || string.IsNullOrEmpty(order_no))
                    {
                        redirect = linkurl("error", "?msg=" + Utils.UrlEncode("出错了，URL传输参数有误！"));
                        return;
                    }
                    //是否需要支持匿名购物
                    userModel = GetUserInfo(); //取得用户登录信息
                    if (orderConfig.anonymous == 0 || order_no.ToUpper().StartsWith("R"))
                    {
                        if (userModel == null)
                        {
                            //用户未登录
                            redirect = linkurl("payment", "?action=login");
                            return;
                        }
                    }
                    else if (userModel == null)
                    {
                        userModel = new Model.users();
                    }
                    //检查订单的类型(充值或购物)
                    if (order_no.ToUpper().StartsWith("R")) //充值订单
                    {
                        rechargeModel = new BLL.user_recharge().GetModel(order_no);
                        if (rechargeModel == null)
                        {
                            redirect = linkurl("error", "?msg=" + Utils.UrlEncode("出错了，订单号不存在或已删除！"));
                            return;
                        }
                        //检查订单号是否已支付
                        if (rechargeModel.status == 1)
                        {
                            redirect = linkurl("payment", "?action=succeed&order_no=" + rechargeModel.recharge_no);
                            return;
                        }
                        //检查支付方式
                        payModel = new BLL.site_payment().GetPaymentModel(rechargeModel.payment_id);
                        if (payModel == null)
                        {
                            redirect = linkurl("error", "?msg=" + Utils.UrlEncode("出错了，支付方式不存在或已删除！"));
                            return;
                        }
                        //检查是否线上支付
                        if (payModel.type == 2)
                        {
                            redirect = linkurl("error", "?msg=" + Utils.UrlEncode("出错了，账户充值不允许线下支付！"));
                            return;
                        }
                        order_amount = rechargeModel.amount; //订单金额
                    }
                    else if (order_no.ToUpper().StartsWith("B")) //商品订单
                    {

                    }
                    else
                    {
                        redirect = linkurl("error", "?msg=" + Utils.UrlEncode("出错了，找不到您要提交的订单类型！"));
                        return;
                    }
                    break;
                case "succeed":
                    //检查订单的类型(充值或购物)
                    if (order_no.ToUpper().StartsWith("R")) //充值订单
                    {
                        rechargeModel = new BLL.user_recharge().GetModel(order_no);
                        if (rechargeModel == null)
                        {
                            redirect = linkurl("error", "?msg=" + Utils.UrlEncode("出错了，订单号不存在或已删除！"));
                            return;
                        }
                    }
                    else if (order_no.ToUpper().StartsWith("B")) //商品订单
                    {

                    }
                    else
                    {
                        redirect = linkurl("error", "?msg=" + Utils.UrlEncode("出错了，找不到您要提交的订单类型！"));
                        return;
                    }
                    break;
            }
        }
    }
}
